package cn.chenfeifx.core.controls;

import cn.chenfeifx.core.controls.skins.CFImageSkin;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
import javafx.scene.image.Image;

/**
 * 自定义Image组件
 * <p>
 * 显示方式：contain, cover, fill, none, scale-down
 */
public class CFImage extends Control {

    public CFImage() {
        setMaxSize(USE_PREF_SIZE, USE_PREF_SIZE);
        setPrefSize(100, 100);
        getStyleClass().add("cf-image");
    }

    public CFImage(Image image) {
        this();
        setImage(image);
    }

    public CFImage(Image image, double width, double height) {
        this(image);
        setPrefSize(width, height);
    }

    public CFImage(Image image, double width, double height, double arc) {
        this(image, width, height);
        setArc(arc);
    }

    /**
     * 圆角
     */
    private final DoubleProperty arc = new SimpleDoubleProperty(0);

    public double getArc() {
        return arc.get();
    }

    public DoubleProperty arcProperty() {
        return arc;
    }

    public void setArc(double arc) {
        this.arc.set(arc);
    }

    /**
     * 图片填充方式
     */
    private final ObjectProperty<Fit> fit = new SimpleObjectProperty<>(Fit.FILL);

    public Fit getFit() {
        return fit.get();
    }

    public ObjectProperty<Fit> fitProperty() {
        return fit;
    }

    public void setFit(Fit fit) {
        this.fit.set(fit);
    }

    /**
     * 图片
     */
    private final ObjectProperty<Image> image = new SimpleObjectProperty<>();

    public Image getImage() {
        return image.get();
    }

    public ObjectProperty<Image> imageProperty() {
        return image;
    }

    public void setImage(Image image) {
        this.image.set(image);
    }

    @Override
    protected Skin<?> createDefaultSkin() {
        return new CFImageSkin(this);
    }

    /**
     * 图片填充方式枚举
     */
    public enum Fit {
        CONTAIN, COVER, FILL, NONE, SCALE_DOWN
    }
}
